﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Interface_Tier;
using System.Configuration;
using Transfer_Object_Tier;

namespace eSolution_HRMSS.Winforms.DanhMuc
{
    public partial class FR_BacLuong : DevExpress.XtraEditors.XtraForm
    {
        string StepCode = "";
        string RankCode = "";
        int new_id = -1;
        bool isUpdate = false;
        IDIC_SALARY_RANKData _IDIC_SALARY_RANKData;
        IDIC_SALARY_STEPData _IDIC_SALARY_STEPData;
        ICommonCode _icommon;
        eSolution_HRMSS.HRMSS_Ultil util = new HRMSS_Ultil();
        public delegate void updatelist();
        public updatelist truyentinhieu;


        public FR_BacLuong(bool _isUpdate)
        {
            InitializeComponent();
            isUpdate = _isUpdate;
        }

        public FR_BacLuong(bool _isUpdate, string stepcode, string rankcode)
        {
            InitializeComponent();
            isUpdate = _isUpdate;
            StepCode = stepcode;
            RankCode = rankcode;
        }

        private void FR_HocVan_Load(object sender, EventArgs e)
        {
            _IDIC_SALARY_RANKData = (IDIC_SALARY_RANKData)Activator.GetObject(typeof(IDIC_SALARY_RANKData), util.ChuoiDIC_SALARY_RANKData);
            _IDIC_SALARY_STEPData = (IDIC_SALARY_STEPData)Activator.GetObject(typeof(IDIC_SALARY_STEPData), util.ChuoiDIC_SALARY_STEPData);
            _icommon = (ICommonCode)Activator.GetObject(typeof(ICommonCode), util.Chuoicommon);
            
            tangid();
            load_ds_ngachluong();
            if (isUpdate)
            {
                //load thong tin update
                loadthongtin();
            }
            else
            {
                txtma.Text = new_id.ToString();
            }
        }
        private void load_ds_ngachluong()
        {
            lu_ngachluong.Properties.DataSource = _IDIC_SALARY_RANKData.DIC_SALARY_RANK_GetList().Tables[0];
            lu_ngachluong.Properties.DisplayMember = "RankName";
            lu_ngachluong.Properties.ValueMember = "RankCode";
        }
        private void tangid()
        {
            new_id = _icommon.Generate_ID_Number_For_Salary_Step(RankCode);
        }

        private void loadthongtin()
        {
            DIC_SALARY_STEP hv = Laythongtin(StepCode, RankCode);
            txtma.Text = StepCode;
            txtten.Text = hv.StepName;
            txtghichu.Text = hv.Description;
            txthesoluong.Text = hv.Coefficient.ToString();
            lu_ngachluong.EditValue = RankCode;
        }

        private DIC_SALARY_STEP Laythongtin(string stepcode, string rankcode)
        {
            DataSet ds = _IDIC_SALARY_STEPData.DIC_SALARY_STEP_Get(int.Parse(stepcode), rankcode);

            DIC_SALARY_STEP hv = new DIC_SALARY_STEP();

            if (ds.Tables.Count>0)
            {
                string ten = ds.Tables[0].Rows[0][2].ToString();
                string heso = ds.Tables[0].Rows[0][3].ToString();
                string ghichu = ds.Tables[0].Rows[0][4].ToString();

                hv.Description = ghichu;
                hv.Coefficient = double.Parse(heso);
                hv.StepName = ten;
            }

            return hv;
        }

        private int Hoc_van_insert()
        {
            string ma = txtma.Text;
            string ten = txtten.Text;
            string ghichu = txtghichu.Text;
            string heso = txthesoluong.Text;
            string rankcode = lu_ngachluong.EditValue.ToString();
            int kq = _IDIC_SALARY_STEPData.DIC_SALARY_STEP_Insert(int.Parse(ma), rankcode, ten, double.Parse(heso), ghichu);
            return kq;
        }

        private int Hoc_van_update()
        {
            string ma = txtma.Text;
            string ten = txtten.Text;
            string ghichu = txtghichu.Text;
            string heso = txthesoluong.Text;
            string rankcode = lu_ngachluong.EditValue.ToString();
            int kq = _IDIC_SALARY_STEPData.DIC_SALARY_STEP_Update(int.Parse(ma), rankcode, ten, double.Parse(heso), ghichu);
            return kq;
        }

        private void btluudong_Click(object sender, EventArgs e)
        {
            if (!validate_data())
                return;

            if (!isUpdate )
            {
                //insert
                if (Hoc_van_insert() >= 0)
                {
                    truyentinhieu();
                    this.Close();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
            else
            {
                //update
                if (Hoc_van_update() >= 0)
                {
                    truyentinhieu();
                    this.Close();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
        }

        private bool validate_data()
        {
            if (txtma.Text.Length == 0 || txtten.Text.Length == 0)
            {
                MessageBox.Show(util.Loi_thieuthongtin);
                return false;
            }
            else
                return true;
        }

        private void clear_form()
        {
            tangid();
            txtghichu.Text = "";
            txthesoluong.Text = "0";
            txtma.Text = new_id.ToString();
            txtten.Text = "";
            isUpdate = false;
        }

        private void btluuthem_Click(object sender, EventArgs e)
        {
            if (!validate_data())
                return;
            if (!isUpdate)
            {
                //insert
                if (Hoc_van_insert() >= 0)
                {
                    truyentinhieu();
                    clear_form();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
            else
            {
                //update
                if (Hoc_van_update() >= 0)
                {
                    truyentinhieu();
                    clear_form();
                }
                else
                    MessageBox.Show(util.Loithaotac);
            }
        }

        private void btdong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lu_ngachluong_EditValueChanged(object sender, EventArgs e)
        {
            if (!isUpdate)
            {
                tangid();
                txtma.Text = new_id.ToString();
            }
        }
    }
}